package dp.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

public class HttpUtil {
	public static String postMap(String url, Map<String, String> paramMap) throws ClientProtocolException, IOException{
		return postMap(url, null, paramMap, "utf-8");
	}
	
	public static String postMap(String url, Map<String, String> paramMap, String charset) throws ClientProtocolException, IOException{
		return postMap(url, null, paramMap, charset);
	}
	
	public static String postMap(String url, Map<String, String> headerMap, 
			Map<String, String> paramMap, String charset) throws ClientProtocolException, IOException{
		String html = "";
		
		CloseableHttpClient http = HttpClients.createDefault();
		HttpPost post = new HttpPost(url); 
		//header
		if(headerMap!=null){
			Iterator iterator = headerMap.entrySet().iterator();
			while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                post.addHeader((String) entry.getKey(), (String)entry.getValue());
            }
		}
		//param
		if(paramMap!=null){
			List<NameValuePair> params = new ArrayList<NameValuePair>();  
			Iterator iterator = paramMap.entrySet().iterator();
        	while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                params.add(new BasicNameValuePair((String) entry.getKey(), (String)entry.getValue()));  
            }
        	post.setEntity(new UrlEncodedFormEntity(params, charset));
		}
		
		try{
			HttpResponse response = http.execute(post);  
		    InputStream in = response.getEntity().getContent();  
		    BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));  
		    String line = null;  
		    int lineIndex = 0;
		    while ((line = reader.readLine()) != null) {  
		    	if(lineIndex>0){
		    		html += "\n"; 
		    	}
		    	html += line;  
		    	lineIndex++;
		    }  
		}
		finally{
			http.close();
		}
		return html;
	}
	
	public static String postString(String url, String bodyString) throws ClientProtocolException, IOException{
		return postString(url, null, bodyString, "utf-8");
	}
	
	public static String postString(String url, String bodyString, String charset) throws ClientProtocolException, IOException{
		return postString(url, null, bodyString, charset);
	}
	
	public static String postString(String url, Map<String, String> headerMap, String bodyString, String charset) throws ClientProtocolException, IOException{
		String html = "";
		
		CloseableHttpClient http = HttpClients.createDefault();
		HttpPost post = new HttpPost(url); 
		//header
		if(headerMap!=null){
			Iterator iterator = headerMap.entrySet().iterator();
			while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                post.addHeader((String) entry.getKey(), (String)entry.getValue());
            }
		}
		//bodyString
		if(bodyString!=null){
			post.setEntity(new StringEntity(bodyString, charset));
		}
		
		try{
			HttpResponse response = http.execute(post);  
		    InputStream in = response.getEntity().getContent();  
		    BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));  
		    String line = null;  
		    int lineIndex = 0;
		    while ((line = reader.readLine()) != null) {  
		    	if(lineIndex>0){
		    		html += "\n"; 
		    	}
		    	html += line;  
		    	lineIndex++;
		    }  
		}
		finally{
			http.close();
		}
		return html;
	}

}
